
#程序入口领取任务进行分配

import json,time
from common.Helper import Helper
from scheduler.ordercase import OrderCase
from scheduler.indexcase import OrderIncrement
from crawler.logistics import Logistics
from crawler.comment import PddComments
from crawler.details import PddDetails
from crawler.score import PddScore
from crawler.notes import PddNotes
from pprint import pprint

# api 初始化任务、增量采集 程序独立接口  | 备注、详情、物流、店铺评分 程序独立接口 | 评价任务单独独立

class RequestTask( object ):

    def __init__(self):

        pass

    def receTask(self,types):



            task =  Helper.redisCase(types)

            # print( '==================',task )

            if task:


                task = json.loads( task.decode() )

                # task = {
                #     "type": 206,
                #     "content": {
                #         "task_page_id": 1,
                #         "task_id": 1,
                #         "owner_id": 186716817,
                #         "start_time": 1561132800,
                #         "end_time": 1561823999,
                #         "page": 1,
                #         "cookie": "api_uid=rBQRGV0ufMgJpwYt0e1qAg==; _nano_fp=XpdjXqXxnqEbnpXyXT_g30sJEPJz34pSjjxCPZK3; PASS_ID=1-tE0KfMKCcjroz5fP24Q1z8JICfav7k/PvdlX7HMr8eSHHkqSndr7qTifW1ovaZhOpVdNMABvQz49PIIagnwYQg_186716817_29159208; JSESSIONID=78C6A09EBE8A2C13DA77D65781573E68"
                #     },
                #     "msg": "采集评价任务"
                # }
                # task = json.loads( Helper.redisCase(types).decode() )

                # print('正在执行 订单列表任务')
                if int(task['type']) == 200:

                    # 执行增量任务
                    print('【API类型任务】执行拼多多SDK订单增量任务')

                    task = (task['content'])

                    i = OrderIncrement()

                    res = i.requestJson(task)

                    return res

                elif int(task['type']) == 201:

                    print('【API类型任务】执行拼多多SDK订单列表任务')
                    # 执行订单列表
                    task = ( task['content'])

                    o = OrderCase()

                    res = o.requestJson( task )

                    return  ( res )

                elif int(task['type']) == 203 :

                    #备注任务
                    print('【爬虫类型任务】正在执行备注任务')

                    # print('执行拼多多SDK订单列表任务')
                    # 执行订单列表
                    task = (task['content'])

                    n = PddNotes()

                    res = n.requestNotes( task )

                    return (res)


                elif int(task['type']) == 204:

                    print('【爬虫类型任务】采集拼多多物流任务')

                    task = (task['content'])

                    l = Logistics()

                    res = l.requestIndex( task )

                    return ( res )


                elif int(task['type']) == 205:
                    # 脱敏数据
                    print('【爬虫类型任务】正在执行脱敏数据任务')

                    task = (task['content'])

                    d = PddDetails()

                    res = d.requestIndex( task )

                    return res


                elif int(task['type']) == 206:

                    resp = {'type':104,'msg':'评价任务','content':{}}

                    #评价任务
                    print('【爬虫类型任务】正在执行评价任务')

                    task = (task['content'])

                    #首次访问是否有评价信息
                    data = Helper.testCommentTask( task )

                    # IP被封 错误代码 40002
                    if not data:

                        resp['content']['content'] = task
                        resp['content']['data'] = {}
                        resp['content']['errcode'] = 40002
                        resp['content']['errmsg'] = 'IP被封'

                        pprint( resp )

                        content = Helper.changeBase64(json.dumps(resp['content']))

                        resp['content'] = content.decode('ascii')

                        return resp

                    elif data['errorMsg'] == '会话已过期':

                        # 返回会话过期
                        resp['content']['content'] = task
                        resp['content']['data'] = {}
                        resp['content']['errcode'] = data['errorCode']
                        resp['content']['errmsg'] =  data['errorMsg']
                        pprint( resp )
                        content = Helper.changeBase64(json.dumps(resp['content']))

                        resp['content'] = content.decode('ascii')

                        return resp

                    else:

                        # 正常执行任务
                        c = PddComments()

                        data = c.requestIndex( task )

                        return data


                elif int(task['type']) == 207:

                    # 执行店铺评分
                    print('【爬虫类型任务】执行店铺评分任务')

                    task = (task['content'])

                    s = PddScore()

                    res = s.requestIndex( task )

                    return  (res)

                else:

                    pass

            else:

                print('暂无领取任务睡眠30s')

                time.sleep(5)


    def main(self):

        while True:

            print( self.receTask('crawler_tasks') )



if __name__ == '__main__':



    r = RequestTask()

    r.receTask('api_tasks')

    # r.main()